# -*- coding:utf-8 -*-

"""
    engine
"""

from flask import Flask, request
import config as config
from enum import Enum
import config.DBMgr as DBMgr
import base64


class SqlType(Enum):
    DeviceInfo = 1

app = config.app
db = DBMgr.DBMgr()

@app.route('/engine/')
def engine():
    return 'Hello engine Center'

@app.route('/engine/collectInfo')
def collectInfo():
    info = request.args.get('info')
    print("\n\n\ninfo1 : " + info)


    info = base64.urlsafe_b64decode(info)
    info = info.decode()
    print("\n\n\ninfo : " + info)
    params = {}
    paramsTemp = info.split(",")
        
    for paramTemp in paramsTemp:
        temp = paramTemp.split("=")
        params[temp[0]] = temp[1]

    sqlStr = generateSql(SqlType.DeviceInfo, params)
    
    effect_row = db.execute(sqlStr)
    db.commit()
    
    
    return ""


def generateSql(type, params):
    sqlStr = ""
    if type == SqlType.DeviceInfo:
        sqlStrPre = "INSERT INTO t_device_info ("
        sqlStrBak = "VALUES ("
        for key, value in params.items():
            sqlStrPre = sqlStrPre + key + ","
            sqlStrBak = sqlStrBak + "'" + value + "'" + ","
        
        sqlStrPre = sqlStrPre.rstrip(',') + ')'
        sqlStrBak = sqlStrBak.rstrip(',') + ");"
        sqlStr = sqlStrPre + sqlStrBak

        print("sqlStr:", sqlStr)
    return sqlStr



